<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>注册页面</title>
		<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
		<link rel="stylesheet" href="css/amazeui.min.css" />
		<script src="js/jquery.min.js" type="text/javascript" charset="utf-8"></script>
		<script src="js/amazeui.min.js"></script>
		<script src="js/sweetalert/dist/sweetalert-dev.js"></script>
		<link rel="stylesheet" href="js/sweetalert/dist/sweetalert.css">
		<script src="js/mui.js" ></script>
 	   <script src="myjs/common.js" ></script>
	   <script src="myjs/reg.js"></script>
	    <script src="js/sweetalert/dist/sweetalert-dev.js"></script>
  <link rel="stylesheet" href="js/sweetalert/dist/sweetalert.css">
	</head>

	<body style="background: url(img/login-bg.jpg) center no-repeat; overflow: hidden;">
		<style type="text/css">
			.am-form {
				font-size: 15px;
			}
		</style>

		   <header data-am-widget="header"
          class="am-header am-header-default ">
          <a href="index.html">
          	<span class="am-icon-angle-left am-left am-icon-md am-fl"></span>
         </a> 
      <h1 class="am-header-title">
   注册
      </h1>
      <div class="am-header-right am-header-nav">
          <a href="login.html">
        <span >登录</span>
      </a>          
      </div>
  </header>

		<div class="am-g am-none" style="margin: 8px 0px; ">
			<span class="am-margin-left-xs">所选工厂:</span>
			<span id="selecteds"></span>
		</div>

		<div class="am-g">
			<form action="" class="am-form">
				<fieldset>
					<div class="am-form-group">
						<label for="doc-select-2">选择工厂：</label>
						<select multiple id="select" class="" onchange="sel()" required>
						</select>
					</div>

					<div class="am-form-group">
						<label for="username">用户名：</label>
						<input type="text" id="username" name="username" onblur="hasU()" placeholder="至少3个字符英文加数字" required onkeyup="value=value.replace(/[^\w\.\/]/ig,'')" />
					</div>

					<div class="am-form-group">
						<div class="am-form-group">
							<label for="phone">手机号：</label>
							<input type="text" id="phone" name="phone" class="" placeholder="输入手机号" required/>
						</div>
					</div>
					<a class="am-btn am-btn-secondary am-btn-block am-round" onclick="reg()">注册</a>
				</fieldset>
			</form>
		</div>

		<script type="text/javascript">
			$(function() {
				$('#select').selected({
					btnWidth: '100%',
					btnStyle: 'default',
					maxHeight: '350px'
				});
			});
			var checkedList = new Array();

			function sel() {
				var select = document.getElementById("select");
				var tex = $("#selecteds").text();
				var str = [];
				for(i = 0; i < select.length; i++) {
					if(select.options[i].selected) {
						str += select[i].text + ',';
						checkedList[i] = select[i].value;
					}
				}
				console.log(checkedList);
				$("#selecteds").html(str);
			}

		//判断用户名是否存在
	function hasU(){
		
		var newusername = $("#username").val();
		if(newusername !="" ){
			$.getJSON("json/locat.json", function(data) {
						var locat = data["url"];
			
					var url = locat+"/wxuser/hasU?newusername="+newusername+"&tm="+new Date().getTime();
			$.ajax({
				   type: "get", cache: false,  
				    url: url,  
				    data: "",  
				    dataType: "text",  
				    success: function (data) {  
				        if (data == "error" || data == "") {  
				        	swal("用户名已存在!");
				        	$("#username").val("");
							$("#username").focus();
				        } 
				    }  
			});
			})
			
		}
		if ($("#username").val().length>0&&$("#username").val().match(/[\x01-\xFF]*/)==false){
			swal('格式不对请重新填写')
		    $("#username").val("");
			$("#username").focus();
		  }
		
		
	}


			function reg() {
				var phone = document.getElementById('phone').value;
				var reg = /^1\d{10}$/;
				var username = document.getElementById('username').value;
				var select = document.getElementById("select").value;
				if(select == "") {
					swal('至少选择一家工厂一个');
				} else if(username.length < 3) {
					swal('用户名，至少三个字符');
					$("#username").focus();
				} else if(!reg.test(phone)) {
					swal("手机号码输入有误");
					$("#phone").focus();
				} else {
					$.getJSON("json/locat.json", function(data) {
						var locat = data["url"];
						var originalID = data["originalID"];
						$.ajax({
							url: locat+"/appClient/appUserReg",
							type: "post",
							data: {
								'factory_IDs': checkedList,
								'username': username,
								'phone': phone,
								'originalID':originalID
							},
							dataType: "json",
							success: function(data) {
								if(data["msg"] == 'success') {
									alert("注册成功,初始化密码为"+data["password"]+"。");
									window.location.href="login.html";
								} else {
									alert("error");
								}
							},
							error: function() {
								swal('服务异常！');
							}
						});

					});

				}
			}
		</script>

	</body>

</html>